<?php
// +----------------------------------------------------------------------
// | 小牛Admin
// +----------------------------------------------------------------------
// | Website: www.xnadmin.cn
// +----------------------------------------------------------------------
// | Author: dav <85168163@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller\curd\service;

class BuildSql
{
    public function build($data, $table_name)
    {
        $html = '';
        foreach ($data as $_data) {
            if ($_data['field'] == '' || $_data['name'] == '') {
                retError('字段名和字段描述不能为空');
            }

            if ( in_array($_data['field'], ['id','create_time','update_time']) ) {
                retError('id、create_time、update_time为默认字段，不需添加');
            }

            //顺便验证其他数据
            if ( in_array($_data['form_type'], ['radio','checkbox','select','switch']) ) {
                $rule = $_data['form_rule'];
                if ( empty($rule) ) {
                    retError('单选规则不能为空');
                }
                $rule = explode("\n", $rule);
                foreach ($rule as $k => $v) {
                    $rule[$k] = trim($v);
                    if ( empty($rule[$k]) ) {
                        retError('单选规则不能为空');
                    }
                    $rule[$k] = explode('|', $rule[$k]);
                    if ( count($rule[$k]) != 2 ) {
                        retError('单选规则格式错误');
                    }
                }
            }

            if ($_data['type'] == 'varchar') {
                $html .= $this->createVarchar($_data).PHP_EOL;
            } elseif ($_data['type'] == 'varchar255') {
                $html .= $this->createVarchar255($_data) . PHP_EOL;
            } elseif ($_data['type'] == 'int') {
                $html .= $this->createInt($_data) . PHP_EOL;
            } elseif ($_data['type'] == 'tinyint') {
                $html .= $this->createTinyint($_data) . PHP_EOL;
            } elseif ($_data['type'] == 'decimal') {
                $html .= $this->createDecimal($_data) . PHP_EOL;
            } elseif ($_data['type'] == 'datetime') {
                $html .= $this->createDatetime($_data) . PHP_EOL;
            } elseif ($_data['type'] == 'date') {
                $html .= $this->createDate($_data) . PHP_EOL;
            } elseif ($_data['type'] == 'text') {
                $html .= $this->createText($_data) . PHP_EOL;
            } elseif ($_data['type'] == 'longtext') {
                $html .= $this->createLongtext($_data) . PHP_EOL;
            }
        }
        $sql = "CREATE TABLE `{$table_name}` (
            `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
            {$html}
            `create_time` int(10) DEFAULT '0' COMMENT '创建时间',
            `update_time` int(10) DEFAULT '0' COMMENT '最近更新时间',
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='{$table_name}';";
        return $sql;
    }

    private function createVarchar($data)
    {
        $str = "`{$data['field']}` varchar(50) NOT NULL DEFAULT '' COMMENT '{$data['name']}',";
        return $str;
    }

    private function createVarchar255($data)
    {
        $str = "`{$data['field']}` varchar(255) NOT NULL DEFAULT '' COMMENT '{$data['name']}',";
        return $str;
    }

    private function createInt($data)
    {
        $str = "`{$data['field']}` int(11) NOT NULL DEFAULT '0' COMMENT '{$data['name']}',";
        return $str;
    }

    private function createTinyint($data)
    {
        $str = "`{$data['field']}` tinyint(1) NOT NULL DEFAULT '0' COMMENT '{$data['name']}',";
        return $str;
    }

    private function createDecimal($data)
    {
        $str = "`{$data['field']}` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '{$data['name']}',";
        return $str;
    }

    private function createDatetime($data)
    {
        $str = "`{$data['field']}` datetime DEFAULT NULL COMMENT '{$data['name']}',";
        return $str;
    }

    private function createDate($data)
    {
        $str = "`{$data['field']}` date DEFAULT NULL COMMENT '{$data['name']}',";
        return $str;
    }

    private function createText($data)
    {
        $str = "`{$data['field']}` text COMMENT '{$data['name']}',";
        return $str;
    }

    private function createLongtext($data)
    {
        $str = "`{$data['field']}` longtext COMMENT '{$data['name']}',";
        return $str;
    }
}
